package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wanmao.test.bean.GCBean;
import com.wanmao.test.dbhelper.DBOpenHelper;



public class GCService {
    private DBOpenHelper dBOpenHelper;

    public GCService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(GCBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("GC_TABLE", null, settingValue(bean));
    }

    public ContentValues settingValue(GCBean bean) {
        ContentValues values = new ContentValues();
        values.put("GC_ID",bean.getGC_ID());
        values.put("GCMC",bean.getGCMC());
        values.put("GCLX",bean.getGCLX());
        values.put("DYDJ",bean.getDYDJ());
        values.put("GCXZ",bean.getGCXZ());
        return values;
    }
    public GCBean findByGCID(String gcid) {
        String sql = "select * from GC_TABLE where GC_ID=?";

        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{gcid});

        while (cursor.moveToNext()){
            return toBean(cursor);
        }
        cursor.close();
        db.close();
        return null;
    }

    private GCBean toBean(Cursor cursor) {
        GCBean bean=new GCBean();
        bean.setGC_ID(cursor.getString(cursor.getColumnIndex("GC_ID")));
        bean.setGCMC(cursor.getString(cursor.getColumnIndex("GCMC")));
        bean.setGCLX(cursor.getString(cursor.getColumnIndex("GCLX")));
        bean.setDYDJ(cursor.getString(cursor.getColumnIndex("DYDJ")));
        bean.setGCXZ(cursor.getString(cursor.getColumnIndex("GCXZ")));

        return bean;
    }


}
